﻿Partial Class PictureView
    Public Class PictureItemCollection
        Implements IList(Of PictureItem)

        Private _pictureView As PictureView
        Private _List As New List(Of PictureItem)
        Friend Sub New(ByVal pictureView As PictureView)
            _pictureView = pictureView
        End Sub

        Public Sub Add(item As PictureItem) Implements ICollection(Of PictureItem).Add
            Me.Add(item, False)
        End Sub

        Friend Sub Add(ByVal item As PictureItem, ByVal Updateing As Boolean)
            Me._List.Add(item)
            item.PictureView = Me._pictureView

            If Updateing = False Then
                Dim info As New FileManagerItem
                info.Id = item.Id
                info.Name = item.Name
                info.LocalPath = item.LocalPath
                Me._pictureView.Files.Add(info)

            End If

            item.GetThumbnailImage()
            If Updateing = False Then
                Me._pictureView.SetValue()
                Me._pictureView.SelectedItem = item
            End If
            Me._pictureView.FlowLayoutPanel1.Controls.Add(item.PictureItem)

        End Sub

        Public Sub Clear() Implements ICollection(Of PictureItem).Clear
            Me.Clear(False)
        End Sub
        Friend Sub Clear(ByVal Updateing As Boolean)
            Me._pictureView.SelectedItem = Nothing
            Me._List.Clear()
            If Updateing = False Then
                Me._pictureView.Files.Clear()
            End If
            Me._pictureView.FlowLayoutPanel1.Controls.Clear()
        End Sub

        Public Function Contains(item As PictureItem) As Boolean Implements ICollection(Of PictureItem).Contains
            Return Me._List.Contains(item)
        End Function

        Public Sub CopyTo(array() As PictureItem, arrayIndex As Integer) Implements ICollection(Of PictureItem).CopyTo
            Me._List.CopyTo(array, arrayIndex)
        End Sub

        Public ReadOnly Property Count As Integer Implements ICollection(Of PictureItem).Count
            Get
                Return Me._List.Count
            End Get
        End Property

        Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of PictureItem).IsReadOnly
            Get
                Return False
            End Get
        End Property

        Public Function Remove(item As PictureItem) As Boolean Implements ICollection(Of PictureItem).Remove
            Dim index As Integer = -1
            For i As Integer = 0 To Me.Count
                If Me.Item(i) Is item Then
                    index = i
                    Exit For
                End If
            Next

            item.PictureItem.Dispose()

            For Each Value As FileManagerItem In Me._pictureView.Files
                If Value.Id = item.Id AndAlso Value.Name = item.Name Then
                    Me._pictureView.Files.Remove(Value)
                    Exit For
                End If
            Next

            Dim boo As Boolean = Me._List.Remove(item)

            Me._pictureView.SetValue()
            If Me._List.Count > index Then
                Me._pictureView.SelectedItem = Me._List(index)
            ElseIf Me._List.Count > 0 Then
                Me._pictureView.SelectedItem = Me._List(Me._List.Count - 1)
            ElseIf Me._List.Count = 0 Then
                Me._pictureView.SelectedItem = Nothing
            End If
            Return boo
        End Function

        Public Function GetEnumerator() As IEnumerator(Of PictureItem) Implements IEnumerable(Of PictureItem).GetEnumerator
            Return Me._List.GetEnumerator
        End Function

        Public Function IndexOf(item As PictureItem) As Integer Implements IList(Of PictureItem).IndexOf
            Return Me._List.IndexOf(item)
        End Function

        Public Sub Insert(index As Integer, item As PictureItem) Implements IList(Of PictureItem).Insert
            Me._List.Insert(index, item)
        End Sub

        Default Friend Property Item(index As Integer) As PictureItem Implements IList(Of PictureItem).Item
            Get
                Return Me._List.Item(index)
            End Get
            Set(value As PictureItem)
                Me._List.Item(index) = value
            End Set
        End Property

        Public Sub RemoveAt(index As Integer) Implements IList(Of PictureItem).RemoveAt
            Me._List.RemoveAt(index)
        End Sub

        Friend Function GetEnumerator1() As IEnumerator Implements IEnumerable.GetEnumerator
            Return Me._List.GetEnumerator()
        End Function


    End Class

End Class